<?php 
session_start();
	require_once ("class/m_branch.php");

	$branch = new M_branch();
	$user=$_SESSION['username'];
 	 
	if (isset($_POST['m'])  &&  $_POST['m']!=""){
		if ($_POST['m']== 'detail'){
			if(isset($_POST['id']) && $_POST['id']!=""){ 
				$id=$_POST['id'];
				//$id=1; json_encode
				echo json_encode($branch->getbranchdetail($id));
			}else{
				echo "Errro";
			}
		}else if ($_POST['m']== 'add'){
			$ar= $_POST['var'];
			$opendate=$ar["OpenDate"];
			list($day, $month, $year) = split('/', $ar["OpenDate"]);
			$ar["OpenDate"]=$year."/".$month."/".$day;
			$ar["CreatedBy"]=$user;
			$ar["CreatedDate"]=date( "Y/m/d H:i:s" );
			//check duplicate
			if ($branch->checkBranch($ar["BranchCode"])=="0"){
				echo "error";
			}
			else{
				
				$branch->insertBarnchToRunning($ar["BranchCode"]);
			
				$id=$branch->InsertBranch($ar);
				$r=$_POST['rows']+1 ;
				$ar["OpenedDate1"]=$opendate;
				$ar["ID"]=$id;
				echo "<tr>".genTable($ar,$r) . "</tr>";
				
				// echo "<tr><td align='right'>".$r."</td>" .
					 // "<td style='display:none'>".$id."</td>" .
					 // "<td align='center'>".$ar["BranchCode"]."</td>" .
					 // "<td align='left'>".$ar["BranchName"]."</td>" .
					 // "<td align='left'>".$ar["Address"]."</td>" .
					 // "<td align='center'>".$ar["Telephone"]."</td>" .
					 // "<td align='center'>".$opendate."</td>" .
					// "<td align='center'><img src='./images/view.png' style='cursor:pointer' class='btnView' border='0'/></td>".
					// "<td align='center'><img src='./images/edit.png' class='btnEdit' style='cursor:pointer' title='Edit' border='0'/></td>".
					// "<td align='center'><img src='./images/remove.png' class='btnDel' style='cursor:pointer' title='Delete' border='0'/></td>" .
				// "</tr>"; 
			}
			
		}else if ($_POST['m']=='edit'){
			if (isset($_POST['var'])){
				$ar=$_POST['var'];
				$flag=isset($ar);
				$opendate = $ar["OpenDate"];
				list($day, $month, $year) = split('/', $ar["OpenDate"]);
				$ar["OpenDate"]=$year."/".$month."/".$day;
				$ar["UpdatedBy"]=$user;
				$ar["UpdatedDate"]=date( "Y/m/d H:i:s" );
				
				$sqlScript="select * from tb_branch where IsActived=1 and ID='". $_POST['id'] ."'";
				$branch->updateBranch($sqlScript, $ar);
				
				$ar["ID"]=$_POST['id'];
				$ar["OpenedDate1"]=$opendate;
				
				echo genTable($ar,$_POST['rows'] );
				
				// echo "<td align='right'>".$_POST['rows']."</td>".
				// "<td style='display:none'>".$_POST['id']."</td>" .
				 // "<td align='center'>".$ar["BranchCode"]."</td>" .
				 // "<td align='left'>".$ar["BranchName"]."</td>" .
				 // "<td align='left'>".$ar["Address"]."</td>" .
				 // "<td align='center'>".$ar["Telephone"]."</td>" .
				 // "<td align='center'>".$opendate."</td>" .
				// "<td align='center'><img src='./images/view.png' style='cursor:pointer' class='btnView' border='0'/></td>".
				// "<td align='center'><img src='./images/edit.png' class='btnEdit' style='cursor:pointer' title='Edit' border='0'/></td>".
				// "<td align='center'><img src='./images/remove.png' class='btnDel' style='cursor:pointer' title='Delete' border='0'/></td>" ;
			}
					
		}else if ($_POST['m']=='del'){
			if(isset($_POST['var'])){
				$o=$_POST['var'];
				$branch->deleBranch($o["id"]);
			}
		}else if ($_POST['m']== 'search'){
			$code=$_POST['code'];
			$name=$_POST['name'];
			$page=$_POST['page'];
	 
			$page=isset($page)?$page:1;
	
			$result=$branch->getBranch($code,$name,$page);
			$rows=$branch->getRowCount();
			$i=1 + (($page-1) * $branch->getListPage());
			
			$html="";
			
			if ($rows>0){
				while($row = $result->FetchRow()){
					If($i%2 <> 0){
						$html .= "<tr class=row1>";
					} else{
						$html .= "<tr>";    
					}
					$html .= genTable($row,$i)."</tr>";
					$i++;
				}
			}else{
				$html="<tr>"
					."<td colspan='9' align='center' ><img src='./images/info.gif'/><b/></td>"
					."</tr>";
			}
			//Return result , rows , page
			$ar_result=array('data'=>$html,
							'rows'=>$rows,
							'totalpage'=>$branch->getTotalPage());
			echo json_encode( $ar_result );//$html;
		}
	}else{
		echo"Error2";
	}

	function genTable($row,$i){
		global $branchScreen;
				$html ="<td align='center'>".$i."</td>";
				$html .="<td style='display:none'>".$row['ID']."</td>";
				$html .="<td align='center'>".$row['BranchCode']."</td>";
				$html .="<td align='center'>".$row['Tax']."</td>";
				$html .="<td align='center'>".$row['BranchName']."</td>";
				$html .="<td align='center'>".$row['Address']."</td>";
				$html .="<td align='center'>".$row['Telephone']."</td>";
				$html .="<td align='center'>".$row['OpenedDate1']."</td>";

				$html .="<td align='center' style='display:none'><img src='./images/view.png' class='btnView' style='cursor:pointer'  title='View' border='0'/></td>";
				
				if ($_SESSION["permission"][$branchScreen]['Edit']=='1') 					
					$html .="<td align='center'><img src='./images/edit.png' class='btnEdit' style='cursor:pointer'  title='Edit' border='0'/></td>";
				else 
					$html .="<td>&nbsp;</td>";
					
				if ($_SESSION["permission"][$branchScreen]['Deleted']=='1') 						
					$html .="<td align='center'><img src='./images/remove.png' class='btnDel' style='cursor:pointer'  title='Delete' border='0'/></td>";
				else 
					$html .="<td>&nbsp;</td>";
			return $html;
	}

?>